package org.jeecg.modules.manager.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.manager.entity.*;
import org.jeecg.modules.manager.mapper.CarMapper;
import org.jeecg.modules.manager.mapper.GoodsTypeMapper;
import org.jeecg.modules.manager.mapper.RepositoryMapper;
import org.jeecg.modules.manager.service.IPurchaseOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.manager.service.OrderGroupService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 采购订单表
 * @Author: jeecg-boot
 * @Date:   2021-11-19
 * @Version: V1.0
 */
@Slf4j
@Api(tags="采购订单表")
@RestController
@RequestMapping("/manager/purchaseOrder")
public class PurchaseOrderController extends JeecgController<PurchaseOrder, IPurchaseOrderService> {
	@Autowired
	private IPurchaseOrderService purchaseOrderService;

	@Autowired
	private GoodsTypeMapper goodsTypeMapper;

	@Autowired
	private CarMapper carMapper;

	@Autowired
	private RepositoryMapper repositoryMapper;

	@Autowired
	OrderGroupService orderGroupService;

	/**
	 * 分页列表查询
	 *
	 * @param
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "采购订单表-分页列表查询")
	@ApiOperation(value="采购订单表-分页列表查询", notes="采购订单表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(OrderGroup orderGroup,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<OrderGroup> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("deleted", DeletedEnum.FALSE);
		if (StringUtils.isNotBlank(orderGroup.getOrderName())){
			queryWrapper.like("order_name",orderGroup.getOrderName());
		}
		if (StringUtils.isNotBlank(orderGroup.getOrderType())){
			queryWrapper.like("order_type",orderGroup.getOrderType());
		}
		Page<OrderGroup> page = new Page<OrderGroup>(pageNo, pageSize);
		IPage<OrderGroup> pageList = orderGroupService.page(page,queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param
	 * @return
	 */
	@AutoLog(value = "采购订单表-添加或修改")
	@ApiOperation(value="采购订单表-添加或修改", notes="采购订单表-添加或修改\n" +
			"注意事项：1.这里面的puroGoodsId是指carInfo，不是car的id。零配件的话就是它的id id 2.只能选一个仓库，不允许出现一个订单组里多个仓库的情况 \n" +
			"必须要有的字段 puroGoodsId puroPrice puroRepoId,这三个必须要有！")
	@PostMapping(value = "/addOrEdit")
	@Transactional(rollbackFor = Exception.class)
	public Result<?> addOrEdit(@RequestBody OrderGroupPurRo old) {
		return purchaseOrderService.addOrEdit(OrderGroupPurVo.makeVo(old),currentUser()) == 1 ? Result.OK("添加或更新成功！"):Result.error("添加或更新失败，请检查数据是否有误，或确认是否有货物！");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "采购订单表-通过id删除")
	@ApiOperation(value="采购订单表-通过id删除 传订单组的id", notes="采购订单表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		return purchaseOrderService.removeAllById(id,currentUser()) == 0 ? Result.OK("删除成功!"):Result.OK("删除失败!");
	}
	 /**
	  * 通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "采购订单表-通过id删除订单组下的一个订单")
	 @ApiOperation(value="采购订单表-通过id删除订单组下的一个订单 传订单的id", notes="采购订单表-通过id删除订单组下的一个订单")
	 @DeleteMapping(value = "/deleteOrder")
	 public Result<?> deleteOrderById(@RequestParam(name="id",required=true) String id) {
		 return Result.ok(purchaseOrderService.removeOrderById(id,currentUser()));
	 }



	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "采购订单表-通过id查询")
	@ApiOperation(value="采购订单表-通过id查询", notes="采购订单表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		List<PuroQueryByIdVo> orderGroup = purchaseOrderService.getPuroById(id);
		return orderGroup != null ? Result.OK(orderGroup):Result.error("查询失败！");
	}



  /**
   * 导出excel
   *
   * @param request
   * @param purchaseOrder
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, PurchaseOrder purchaseOrder) {
      return super.exportXls(request, purchaseOrder, PurchaseOrder.class, "采购订单表");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, PurchaseOrder.class);
  }

}
